package com.tuanzi.loan.web.bean.statistics;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.joda.time.DateTime;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.common.log.LogService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.starbaba.model.utils.JSONUtils;
import com.starbaba.redis.JedisUtils;
import com.tuanzi.loan.business.entity.family.RegisterRecommendActivity;
import com.tuanzi.loan.business.service.statistics.CommActivityRegisterNewStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.CommActivityRegisterStatRequest;
import com.tuanzi.loan.vo.response.statistics.CommActivityRegisterStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.CommActivityRegisterNewStatistisLazyDataModel;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.HttpConnectionUtils;

/**
 * 新活动通用统计
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CommActivityRegisterStatBean extends JSFListingBean<CommActivityRegisterStatRequest, CommActivityRegisterStatResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    CommActivityRegisterNewStatService commActivityRegisterNewStatService;
    
    private StreamedContent xlsFile;

    private List<CommProductPrdid> productList;
    private List<CommProductChannel> channelList;
    private List<CommPlatform> platformList;
    private List<RegisterRecommendActivity> activityTypeList;
    
    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
    	InputStream input = new ByteArrayInputStream(commActivityRegisterNewStatService.getExcel($).getBytes("GBK"));
		String fileName = System.currentTimeMillis() + ".csv";
		xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
    	return xlsFile;
    }
    

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }

    private void initList(){
        productList = commActivityRegisterNewStatService.getProductList();
        channelList = commActivityRegisterNewStatService.getChannelList();
        platformList = commActivityRegisterNewStatService.getPlatformList();
        activityTypeList = commActivityRegisterNewStatService.getActivityTypeList();
    }
    
    public void flushStatData() {
    	//统计当天的数据
		String cdate = DateUtils.getCurrentDate();
		String url = "http://dkzj.moneyfanli.com/loan_timer/time?funid=65&time=" + cdate;
		String response = HttpConnectionUtils.connection(url, HttpConnectionUtils.GET);
		
		LogService.info("即时统计通用活动结果： " + response);
		JsonParser parser = new JsonParser();
		JsonObject jsonObject = parser.parse(response).getAsJsonObject();
		jsonObject = jsonObject.getAsJsonObject("result");
		Integer code = JSONUtils.checkAsInt(jsonObject, "status");
		if(code == 1){
			addInfoMessage("即时统计数据成功");
		}else{
			addInfoMessage("即时统计数据失败");
		}
    }

    public void onFlushCache(){
    	commActivityRegisterNewStatService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new CommActivityRegisterNewStatistisLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new CommActivityRegisterStatRequest();
        
        // 初始化默认当前日期
        Date date = null;
        try {
			date = sdf.parse(sdf.format(new DateTime().plusDays(-1).toDate()));
			$.setStartDate(date);
	        $.setEndDate(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        lazyDataModel = new CommActivityRegisterNewStatistisLazyDataModel($);
    }
    
}




